<?php
/**
 * Created by PhpStorm.
 * User: apple
 * Date: 17/2/21
 * Time: 13:39
 */
namespace Admin\Controller;
use Common\Controller\AdminbaseController;

class CourseController extends AdminbaseController{

    //添加课程系列
    public function add(){
        //获取所有讲师
        $lectures=M('lecture')->where("status=0")->select();
        $this->assign('lectures',$lectures);
        $this->display();
    }

    public function add_post(){
        $data['create_at']=date("y-m-d h:i:s",time());
        if(IS_POST){
            if (M('course_series')->create()!==false) {
                if (M('course_series')->add($_POST,$data)!==false) {
                    $this->success("添加成功！", U("Course/index"));
                } else {
                    $this->error("添加失败！");
                }
            } else {
                $this->error(M('course_series')->getError());
            }
        }
    }

    //课程系列列表
    public function index(){
        $where=array();
        $keyword=I("keyword");

        if(!empty($keyword)){
            $where["series_name"]=array("like","%$keyword%");
        }
        M('course_series')->where($where);
        $count=M('course_series')->count();
        $page=$this->page($count,20);

        $posts=M('course_series')->alias('cs')->where($where)
            ->join('left join hs_lecture as l on cs.lecture_id=l.lecture_id')
            ->limit($page->firstRow,$page->listRows)->order("create_at DESC")->select();
        $this->assign("page",$page->show());
        $this->assign("formget",array_merge($_GET,$_POST));
        $this->assign("posts",$posts);

        $this->display();
    }

    //课程系列删除
    public function delete(){
        $series_id=I('id');
        $count=M('course')->where("series_id=$series_id")->count();
        if($count>0){
            exit(json_encode(array("status"=>false,"message"=>"该课程系列下有课程,不能删除")));
        }else{
            $result=M('course_series')->where("id=$series_id")->delete();
            if($result!==false){
                exit(json_encode(array("status"=>true,"message"=>"删除成功")));
            }else{
                exit(json_encode(array("status"=>false,"message"=>"删除失败")));
            }
        }
    }

    //课程系列修改
    public function edit(){
        $id=I('id');
        //课程系列一条记录
        $info=M('course_series')->where("id=$id")->find();
        $this->assign('info',$info);
        //所有讲师
        $lectures=M('lecture')->where("status=0")->select();
        $this->assign('lectures',$lectures);
        $this->display();
    }

    public function edit_post(){
        $series_id=I('series_id');
        if(IS_POST){
            //如果未选择图片
            if(empty($_POST['icon'])){
                $data['series_name']=$_POST['series_name'];
                $data['series_intro']=$_POST['series_intro'];
                $data['lecture_id']=$_POST['lecture_id'];
                $result=M('course_series')->where("id=$series_id")->save($data);
                if($result!==false){
                    $this->success("保存成功！",U("Course/index"));
                }else {
                    $this->error("保存失败！");
                }
            }else{
                $result=M('course_series')->where("id=$series_id")->save($_POST);
                if ($result!==false) {
                    $this->success("保存成功！",U("Course/index"));
                } else {
                    $this->error("保存失败！");
                }
            }
        }
    }

    //课程列表
    public function course_index(){
        $series_id=I('series_id');
        $where=array();
        $keyword=I("keyword");
        if(!empty($keyword)){
            $where["title"]=array("like","%$keyword%");
        }
        if(!empty($series_id)){
            $where['series_id']=$series_id;
        }
        M('course')->where($where);
        $count=M('course')->count();
        $page=$this->page($count,20);

        $posts=M('course')->where($where)->limit($page->firstRow,$page->listRows)->order("create_at DESC")->select();
        $this->assign("page",$page->show());
        $this->assign("formget",array_merge($_GET,$_POST));
        $this->assign("posts",$posts);

        $this->display();
    }

    //添加课程
    public function add_course(){
        $series_id=I('series_id');
        $this->assign('series_id',$series_id);
        $this->display();
    }

    public function add_course_post(){
        $data['title']=I('title');
        $data['video_url']=I('video_url');
        $series_id=I('series_id');
        $data['series_id']=$series_id;
        $data['create_at']=date('y-m-d h:i:s',time());
        $result=M('course')->add($data);
        if($result!==false){
            exit(json_encode(array("status"=>true,"message"=>"添加成功")));
        }else{
            exit(json_encode(array("status"=>false,"message"=>'添加失败')));
        }
    }

    //课程修改
    public function edit_course(){
        $id=I('id');
        $course=M('course')->where("id=$id")->find();
        $this->assign('course',$course);
        $this->display();
    }

    public function edit_course_post(){
        $data['title']=I('title');
        $id=I('id');
        $data['video_url']=I('video_url');
        $result=M('course')->where("id=$id")->save($data);
        if($result!==false){
            exit(json_encode(array("status"=>true,"message"=>'保存成功')));
        }else{
            exit(json_encode(array("status"=>false,"message"=>'保存失败')));
        }
    }

    //课程删除
    public function delete_course(){
        $id=I('id');
        $result=M('course')->delete($id);
        if($result!==false){
            exit(json_encode(array("status"=>true,"message"=>"删除成功")));
        }else{
            exit(json_encode(array("status"=>false,"message"=>"删除失败")));
        }

    }

}